package com.kuqi.crs.workbench.web.controller;

import com.kuqi.crs.settings.domain.User;
import com.kuqi.crs.settings.service.UserService;
import com.kuqi.crs.settings.service.impl.UserServiceImpl;
import com.kuqi.crs.util.DateTimeUtil;
import com.kuqi.crs.util.PrintJson;
import com.kuqi.crs.util.ServiceFactory;
import com.kuqi.crs.util.UUIDUtil;
import com.kuqi.crs.vo.PaginationVO;
import com.kuqi.crs.workbench.domain.Clue;
import com.kuqi.crs.workbench.service.ClueService;
import com.kuqi.crs.workbench.service.Impl.ClueServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:kuqi
 * @createTime:2021/10/6 15:48
 * @description:
 */
@WebServlet(value = {
        "/workbench/clue/getUserList.do",
        "/workbench/clue/save.do",
        "/workbench/clue/getClueList.do"

})
public class ClueController extends HttpServlet{
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if ("/workbench/clue/getUserList.do".equals(request.getServletPath())){
            getUserList(request,response);
        }else if("/workbench/clue/save.do".equals(request.getServletPath())){
            save(request,response);
        }else if("/workbench/clue/getClueList.do".equals(request.getServletPath())){
            getClueList(request,response);
        }
    }

    private void getClueList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("获取线索信息列表");

        //获取前端传递的当前页码，以及当前页码显示的线索信息数
        String pageCurrentStr = request.getParameter("pageCurrent");//当前页码
        Integer pageCurrent = Integer.valueOf(pageCurrentStr);
        String pageSizeStr = request.getParameter("pageSize");//每页显示的线索信息数
        Integer pageSize = Integer.valueOf(pageSizeStr);
        String appellation = request.getParameter("appellation");//名称
        String company = request.getParameter("company");
        String phone = request.getParameter("phone");
        String owner = request.getParameter("owner");
        String source = request.getParameter("source");
        String mphone = request.getParameter("mphone");
        String state = request.getParameter("state");

        //计算略过数
        Integer pageSkip = (pageCurrent-1)*pageSize;

        //将数据封装到字典里
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pageSkip",pageSkip);
        map.put("pageSize",pageSize);
        map.put("appellation",appellation);
        map.put("company",company);
        map.put("phone",phone);
        map.put("owner",owner);
        map.put("source",source);
        map.put("mphone",mphone);
        map.put("state",state);

        //创建service对象
        ClueService cs = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        //将返回的数据封装到dataListVo中
        PaginationVO<Clue> vo = cs.getClueList(map);;

        //将数据返回给前端转换成json格式
        PrintJson.printJsonObj(response,vo);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("存储线索信息");

        //获取用户输入的线索数据
        String id = UUIDUtil.getUUID();
        String fullname =request.getParameter("fullname");
        String appellation =request.getParameter("appellation");
        String owner =request.getParameter("owner");
        String company =request.getParameter("company");
        String job =request.getParameter("job");
        String email =request.getParameter("email");
        String phone =request.getParameter("phone");
        String website =request.getParameter("website");
        String mphone =request.getParameter("mphone");
        String state =request.getParameter("state");
        String source =request.getParameter("source");
        String createBy = ((User)request.getSession().getAttribute("user")).getName();
        String createTime = DateTimeUtil.getSysTime();
        String description =request.getParameter("description");
        String contactSummary =request.getParameter("contactSummary");
        String nextContactTime = request.getParameter("nextContactTime");
        String address = request.getParameter("address");

        //创建线索对象
        Clue c = new Clue();
        //将前端获取的数据封装到clue对象中
        c.setId(id);
        c.setFullname(fullname);
        c.setAppellation(appellation);
        c.setOwner(owner);
        c.setCompany(company);
        c.setJob(job);
        c.setEmail(email);
        c.setPhone(phone);
        c.setWebsite(website);
        c.setMphone(mphone);
        c.setState(state);
        c.setSource(source);
        c.setCreateBy(createBy);
        c.setCreateTime(createTime);
        c.setDescription(description);
        c.setContactSummary(contactSummary);
        c.setNextContactTime(nextContactTime);
        c.setAddress(address);

        //创建CludService对象
        ClueService clueService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());

        //调用service对象的方法
        boolean flag = clueService.save(c);
        System.out.println(flag);

        //返回给前端
        PrintJson.printJsonFlag(response,flag);
    }

    private void getUserList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("获取所有用户信息列表");

        //模块是在线索模块中，但是处理的业务是用户的业务，所以创建的UserService业务
        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> userList = us.getUserList();

        PrintJson.printJsonObj(response,userList);
    }
}
